package com.example.common.listener;

import com.example.common.constant.MqQueueNameConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * 监听日志存储请求
 *
 * @author: yukong
 * @date: 2018/11/14 15:24
 */
@Slf4j
@Component
public class SysLogReceiveListener {

//    @Autowired
//    private SysLogService sysLogService;
//    @Autowired
//    private SysLogMapper sysLogMapper;

    /**
     * 日志队列消费端，存数据库
     *
//     * @param sysLogDTO
     * @param channel
     * @param message
     */
    @RabbitListener(queues = MqQueueNameConstant.SYS_LOG_QUEUE)
    public void handler(String methodName, Channel channel, Message message) throws IOException {
        log.info("系统日志消费端成功消费信息：sysLog={}", methodName);
        // 确认消息接受
//        SysLog sysLog = new SysLog();
//        BeanUtils.copyProperties(sysLogDTO, sysLog);
//        Long id = sysLogMapper.seqAlways();
//        sysLog.setId(id);
//        sysLog.setCreateTime(new Date());
//        if(sysLog.getException() !=null && sysLog.getException().length() > 3000){
//            sysLog.setException(sysLog.getException().substring(0,3000));
//        }
//        sysLogService.save(sysLog);
    }

}
